選択要素の操作

您所在的位置:网站首页 selenium select by 選択要素の操作

選択要素の操作

#選択要素の操作 | 来源: 网络整理| 查看: 265

選択要素の操作選択リストには、他の要素と比較して特別な動作があります。

The Select object will now give you a series of commands that allow you to interact with a element.

If you are using Java or .NET make sure that you’ve properly required the support package in your code. See the full code from GitHub in any of the examples below.

Note that this class only works for HTML elements select and option. It is possible to design drop-downs with JavaScript overlays using div or li, and this class will not work for those.

Types

Select methods may behave differently depending on which type of element is being worked with.

Single select

This is the standard drop-down object where one and only one option may be selected.

One Two Four Still learning how to count, apparently Multiple select

This select list allows selecting and deselecting more than one option at a time. This only applies to elements with the multiple attribute.

Eggs Ham Sausages Onion gravy Create class

First locate a element, then use it to initialize a Select object. Note that as of Selenium 4.5, you can’t create a Select object if the element is disabled.

WebElement selectElement = driver.findElement(By.name("selectomatic")); Select select = new Select(selectElement); View full example on GitHub select_element = driver.find_element(By.NAME, 'selectomatic') select = Select(select_element) View full example on GitHub var selectElement = driver.FindElement(By.Name("selectomatic")); var select = new SelectElement(selectElement); View full example on GitHub select_element = driver.find_element(name: 'selectomatic') select = Selenium::WebDriver::Support::Select.new(select_element) View full example on GitHub const selectElement = await driver.findElement(By.name('selectomatic')) const select = new Select(selectElement) View full example on GitHub val selectElement = driver.findElement(By.name("selectomatic")) val select = Select(selectElement) View full example on GitHubList options

There are two lists that can be obtained:

All options

Get a list of all options in the element:

List optionList = select.getOptions(); View full example on GitHub option_list = select.options View full example on GitHub IList optionList = select.Options; View full example on GitHub option_list = select.options View full example on GitHub const optionList = await select.getOptions() View full example on GitHub val optionList = select.getOptions() View full example on GitHubSelected options

Get a list of selected options in the element. For a standard select list this will only be a list with one element, for a multiple select list it can contain zero or many elements.

List selectedOptionList = select.getAllSelectedOptions(); View full example on GitHub selected_option_list = select.all_selected_options View full example on GitHub IList selectedOptionList = select.AllSelectedOptions; View full example on GitHub selected_option_list = select.selected_options View full example on GitHub const selectedOptionList = await select.getAllSelectedOptions() View full example on GitHub val selectedOptionList = select.getAllSelectedOptions() View full example on GitHubSelect option

The Select class provides three ways to select an option. Note that for multiple select type Select lists, you can repeat these methods for each element you want to select.

Text

Select the option based on its visible text

select.selectByVisibleText("Four"); View full example on GitHub select.select_by_visible_text('Four') View full example on GitHub select.SelectByText("Four"); View full example on GitHub select.select_by(:text, 'Four') View full example on GitHub await select.selectByVisibleText('Four') View full example on GitHub select.selectByVisibleText("Four") View full example on GitHubValue

Select the option based on its value attribute

select.selectByValue("two"); View full example on GitHub select.select_by_value('two') View full example on GitHub select.SelectByValue("two"); View full example on GitHub select.select_by(:value, 'two') View full example on GitHub await select.selectByValue('two') View full example on GitHub select.selectByValue("two") View full example on GitHubIndex

Select the option based on its position in the list

select.selectByIndex(3); View full example on GitHub select.select_by_index(3) View full example on GitHub select.SelectByIndex(3); View full example on GitHub select.select_by(:index, 3) View full example on GitHub await select.selectByIndex(3) View full example on GitHub select.selectByIndex(3) View full example on GitHubDisabled options

Selenium v4.5

Options with a disabled attribute may not be selected.

Enabled Disabled Assertions.assertThrows(UnsupportedOperationException.class, () -> { select.selectByValue("disabled"); }); View full example on GitHub with pytest.raises(NotImplementedError): select.select_by_value('disabled') View full example on GitHub Assert.ThrowsException(() => select.SelectByValue("disabled")); View full example on GitHub expect { select.select_by(:value, 'disabled') }.to raise_exception(Selenium::WebDriver::Error::UnsupportedOperationError) View full example on GitHub await assert.rejects(async () => { await select.selectByValue("disabled") }, { name: 'UnsupportedOperationError', View full example on GitHub Assertions.assertThrows(UnsupportedOperationException::class.java) { select.selectByValue("disabled") } View full example on GitHubDe-select option

Only multiple select type select lists can have options de-selected. You can repeat these methods for each element you want to select.

select.deselectByValue("eggs"); View full example on GitHub select.deselect_by_value('eggs') View full example on GitHub select.DeselectByValue("eggs"); View full example on GitHub select.deselect_by(:value, 'eggs') View full example on GitHub await select.deselectByValue('eggs') View full example on GitHub select.deselectByValue("eggs") View full example on GitHub最終更新 May 9, 2023: fix ruby linting issues (#1380) (a6b704a4dc3)


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3